<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="formData.id?'编辑部门':'新增部门'" :visible="isShowDialog" @close="btnNo">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="addDepts" :model="formData" label-width="120px" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择">
          <el-option
            v-for="user in employess"
            :key="user.id"
            :value="user.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click.native="btnOk">确定</el-button>
        <el-button size="small" @click="btnNo">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getEmployess } from '@/api/employess'
import { addDepartment, getDeptsDatil, editDeptsDatil, getDepartment } from '@/api/department'
export default {
  props: {
    isShowDialog: {
      type: Boolean,
      default: false
    },
    nodeData: {
      type: Object,
      required: true
    }
  },
  data() {
    const chekRepeatCode = async(rule, val, callback) => {
      const { depts } = await getDepartment()
      console.log(depts)
      let isRepeat
      if (this.formData.id) {
        isRepeat = depts.some(item => item.code === val && item.id !== this.formData.id)
      } else {
        isRepeat = depts.some(item => item.code === val)
      }
      if (isRepeat) {
        callback(new Error('部门编码必须全公司统一'))
      } else {
        callback()
      }
    }
    const chekRepeatName = async(rule, val, callback) => {
      const { depts } = await getDepartment()
      console.log(depts)
      let isRepeat
      if (this.formData.id) {
        isRepeat = depts
          .filter(item => item.pid === this.formData.id)
          .some(item => item.name === val && item.id !== this.formData.id)
      } else {
        isRepeat = depts
          .filter(item => item.pid === this.nodeData.id)
          .some(item => item.name === val)
      }
      if (isRepeat) {
        callback(new Error('部门名字必须全公司统一'))
      } else {
        callback()
      }
    }
    return {
      formData: {
        code: '',
        introduce: '',
        manager: '',
        name: ''
      },
      rules: {
        code: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { max: 20, message: '最多20个', trigger: 'blur' },
          { validator: chekRepeatCode, trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { max: 202, message: '最多200个', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { max: 20, message: '最多20个', trigger: 'blur' },
          { validator: chekRepeatName, trigger: 'blur' }
        ]
      },
      employess: []
    }
  },
  async created() {
    this.employess = await getEmployess()
  },
  methods: {
    async btnOk() {
      await this.$refs.addDepts.validate()
      // console.log(67, this.nodeData.id)
      if (this.formData.id) {
        await editDeptsDatil(this.formData)
      } else {
        await addDepartment({
          ...this.formData,
          pid: this.nodeData.id
        })
      }
      // 关闭弹窗
      this.$emit('addDepts')
      // 更新页面
      this.$emit('updataPage')
      // 重置输入框
      this.formData = {
        code: '',
        introduce: '',
        manager: '',
        name: ''
      }
    },
    btnNo() {
      this.formData = {
        code: '',
        introduce: '',
        manager: '',
        name: ''
      }
      this.$refs.addDepts.resetFields()
      this.$emit('addDepts')
    },
    async getDeptsDetail(nodeData) {
      this.formData = await getDeptsDatil(nodeData.id)
      console.log('被点击了', this.formData)
    }
  }
}
</script>

<style>

</style>
